package com.wd.health.view.activity.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.baidu.idl.face.platform.FaceStatusNewEnum;
import com.baidu.idl.face.platform.model.ImageInfo;
import com.baidu.idl.face.platform.ui.FaceDetectActivity;
import com.baidu.idl.face.platform.ui.utils.IntentUtils;
import com.wd.health.R;
import com.wd.health.app.App;
import com.wd.health.model.bean.FaceLoginBean;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.utils.RsaCoder;
import com.wd.health.utils.Utils;
import com.wd.health.view.widget.TimeoutDialog;

import java.util.HashMap;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class FaceDetectExpActivity extends FaceDetectActivity implements TimeoutDialog.OnTimeoutDialogClickListener {

    private TimeoutDialog mTimeoutDialog;
    private String bitmap;
    private String userName;
    private String jiGuangPwd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 添加至销毁列表
        App.addDestroyActivity(FaceDetectExpActivity.this, "FaceDetectExpActivity");
    }

    @Override
    public void onDetectCompletion(FaceStatusNewEnum status, String message, HashMap<String, ImageInfo> base64ImageCropMap, HashMap<String, ImageInfo> base64ImageSrcMap) {
        super.onDetectCompletion(status, message, base64ImageCropMap, base64ImageSrcMap);
        if (status == FaceStatusNewEnum.OK && mIsCompletion){
            // showMessageDialog("人脸图像采集", "采集成功");
            IntentUtils.getInstance().setBitmap(mBmpStr);
            bitmap = IntentUtils.getInstance().getBitmap();
            Log.e(TAG,bitmap.toString());
            faceLogin();
            jiGuangLogin();
            Intent intent = new Intent(FaceDetectExpActivity.this,
                    CollectionSuccessExpActivity.class);
            intent.putExtra("destroyType", "FaceDetectExpActivity");
            startActivity(intent);
        }else if (status == FaceStatusNewEnum.DetectRemindCodeTimeout) {
            if (mViewBg != null) {
                mViewBg.setVisibility(View.VISIBLE);
            }
            showMessageDialog();
        }
    }

    private void jiGuangLogin() {
        try {
            String s = RsaCoder.decryptByPublicKey(jiGuangPwd);
            String sss = Utils.md5(s);
            JMessageClient.login(userName, sss, new BasicCallback() {
                @Override
                public void gotResult(int i, String s) {
                    if (i == 0){
                        Log.e("TAG", "gotResult:"+"极光登录成功");
                    }else {
                        Log.e("TAG", "gotResult:"+"极光登录失败");
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void faceLogin() {
        RetrofitManager.getInstance().postFaceLoginData(bitmap, new Observer<FaceLoginBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(FaceLoginBean faceLoginBean) {
                if (faceLoginBean.getStatus().equals("0000")){
                    Toast.makeText(FaceDetectExpActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    String email = faceLoginBean.getResult().getEmail();
                    String headPic = faceLoginBean.getResult().getHeadPic();
                    int userId = faceLoginBean.getResult().getUserId();
                    String sessionId = faceLoginBean.getResult().getSessionId();
                    String nickName = faceLoginBean.getResult().getNickName();
                    userName = faceLoginBean.getResult().getUserName();
                    jiGuangPwd = faceLoginBean.getResult().getJiGuangPwd();
                    SharedPreferences user = getSharedPreferences("user", Context.MODE_PRIVATE);
                    user.edit()
                            .putString("email",email)
                            .putString("headPic",headPic)
                            .putInt("userId",userId)
                            .putString("sessionId",sessionId)
                            .putString("nickName",nickName)
                            .commit();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void showMessageDialog() {
        mTimeoutDialog = new TimeoutDialog(this);
        mTimeoutDialog.setDialogListener(this);
        mTimeoutDialog.setCanceledOnTouchOutside(false);
        mTimeoutDialog.setCancelable(false);
        mTimeoutDialog.show();
        onPause();
    }

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public void onRecollect() {
        if (mTimeoutDialog != null) {
            mTimeoutDialog.dismiss();
        }
        if (mViewBg != null) {
            mViewBg.setVisibility(View.GONE);
        }
        onResume();
    }

    @Override
    public void onReturn() {
        if (mTimeoutDialog != null) {
            mTimeoutDialog.dismiss();
        }
        finish();
    }
}